package modelTest.model.collision_engines;

import java.util.Iterator;

import modelTest.model.CollisionEngine;
import modelTest.model.Coordinate;
import modelTest.model.GameStatus;
import modelTest.model.Ship;
import modelTest.model.mobs.Mob;

/**
 * Semplice motore di collisioni che funziona in 2D.
 * @author Max
 *
 */
public class Simple2DCollisionEngine implements CollisionEngine, Runnable {
	
	
	private GameStatus gameStatus;
	
	public Simple2DCollisionEngine(GameStatus gameStatus) {
		this.gameStatus = gameStatus;
	}
	
	
	
	
	
	@Override
	public void run() {
		
		String ID;
		
		while (true) {
			
//			mobIterator = status.getMobsIterator(true);
//			while (mobIterator.hasNext()) {
//				mobIterator.next().calculateNewCoordinate();;
//			}
			
			if (gameStatus.isRunning()) {
				Iterator<String> IDs = gameStatus.getMobsMapSet().iterator();
				while (IDs.hasNext()) {
					ID = IDs.next();
					Mob mob = gameStatus.getMob(ID);
					int distance = getDistance(gameStatus.getUserShip().getCoordinate(), mob.getCoordinate());
					
					if(distance < Ship.COLLISION_RAY + Mob.COLLISION_RAY) {
						
						//COLLISION DETECTED!
						System.out.println("Collision detected!");
					}
					
				
				}
			}
			
			try {
				Thread.sleep(10);								
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
	private int getDistance(Coordinate shipCoord, Coordinate mobCoord) {
		int tmp = (shipCoord.getX() - mobCoord.getX()) * (shipCoord.getX() - mobCoord.getX());
		tmp += (shipCoord.getY() - mobCoord.getY()) * (shipCoord.getY() - mobCoord.getY());
		return (int) Math.sqrt(tmp);
	}

}
